/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * jpVisualizaNuevaOrden.java
 *
 * Created on 02-ago-2010, 11:52:08
 */

package Ordenes;

import administracion.delegate.GestionPedidosDelegate;
import administracion.helper.Listas;
import administracion.helper.Reportes;
import administracion.helper.Utiles_MensajesDialogo;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import suministros.ejb30.helper.EmailVacioException;

/**
 *
 * @author Luis_Angel
 */
public class jpVisualizaNuevaOrden extends javax.swing.JPanel {

     private JDialog V_padre;
     private String nro_orden;
     private String cod_proveedor;
     private int tipo;
    /** Creates new form jpVisualizaNuevaOrden */
    public jpVisualizaNuevaOrden( JDialog jdPadre, String nro_orden, int tipo, String cod_proveedor) {
       //HiloEnvioEmail hilo = new HiloEnvioEmail();
      // hilo.start();
        this.V_padre=jdPadre;
        this.nro_orden=nro_orden;
        this.tipo = tipo;
        this.cod_proveedor = cod_proveedor;

        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jbEnvio = new javax.swing.JButton();

        jLabel1.setFont(new java.awt.Font("Arial", 0, 12));
        jLabel1.setText("Orden registrada Correctamente");

        jLabel2.setFont(new java.awt.Font("Arial", 0, 12));
        jLabel2.setText("Puede ver el documento Generado");

        jButton1.setFont(new java.awt.Font("Arial", 0, 12));
        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconoscomb/16x16ozygen/actions/document_open.png"))); // NOI18N
        jButton1.setText("VER DOCUMENTO");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setFont(new java.awt.Font("Arial", 0, 12));
        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconoscomb/16x16ozygen/actions/button_cancel.png"))); // NOI18N
        jButton2.setText("CERRAR");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jbEnvio.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconoscomb/16x16ozygen/actions/mail_send.png"))); // NOI18N
        jbEnvio.setText("ENVIAR A PROVEEDOR");
        jbEnvio.setEnabled(false);
        jbEnvio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbEnvioActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(128, Short.MAX_VALUE)
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 211, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(99, 99, 99))
            .addGroup(layout.createSequentialGroup()
                .addGap(129, 129, 129)
                .addComponent(jButton2)
                .addGap(127, 127, 127))
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addComponent(jButton1)
                .addGap(18, 18, 18)
                .addComponent(jbEnvio, javax.swing.GroupLayout.PREFERRED_SIZE, 182, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(32, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(137, Short.MAX_VALUE)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 245, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(56, 56, 56))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButton1, jButton2, jbEnvio});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(16, 16, 16)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jButton1)
                    .addComponent(jbEnvio))
                .addGap(20, 20, 20)
                .addComponent(jButton2)
                .addGap(20, 20, 20))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        this.V_padre.dispose();
        int tipo_orden =  Listas.getTipo_porTablaYCodigo("tipo_Orden","01").getId_tabla_tipo();
        if(tipo==tipo_orden)
               Reportes.verOrden(nro_orden);
        else
               Reportes.verOrdenServicio(nro_orden);
       //Reportes.verOrdenGenerada(this.reporte);
}//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
       
        this.V_padre.dispose();
}//GEN-LAST:event_jButton2ActionPerformed

    private void jbEnvioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbEnvioActionPerformed
        try {
            if(GestionPedidosDelegate.getInstance().generaDocumentoAEmail(nro_orden, tipo, cod_proveedor))
                Utiles_MensajesDialogo.mensajeExito();
            this.jbEnvio.setEnabled(false);
        } catch (EmailVacioException ex) {
             Utiles_MensajesDialogo.mensajeError(ex.getMessage());
        } catch (Exception ex) {
             Utiles_MensajesDialogo.mensajeError(ex.getMessage());
        }
    }//GEN-LAST:event_jbEnvioActionPerformed


    class HiloEnvioEmail extends Thread{
        HiloEnvioEmail(  ){
        }
        
        public void run(){
           try {
                if(GestionPedidosDelegate.getInstance().generaDocumentoAEmail(nro_orden, tipo, cod_proveedor))
                    Utiles_MensajesDialogo.mensajeExito();
            } catch (EmailVacioException ex) {
                 Utiles_MensajesDialogo.mensajeError(ex.getMessage());
            } catch (Exception ex) {
                 Utiles_MensajesDialogo.mensajeError(ex.getMessage());
            }
        }

    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JButton jbEnvio;
    // End of variables declaration//GEN-END:variables

}
